package com.mvc4g.example.mockup.client.view.views;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Event;
import com.mvc4g.example.mockup.client.Session;
import com.mvc4g.example.mockup.client.bean.EmailBean;

public class EmailContentView extends AbstractView {

	private int currentIndex = -1;
	
	private Grid grid = null;
	private Button previous = null;
	private Button next = null;

	@Override
	public void init() {
		// Nothing to do
	}

	public void displayEmail(EmailBean email, int currentIndex, boolean displayPrevious,
			boolean displayNext) {
		
		this.currentIndex = currentIndex;
		
		grid.setText(0, 1, email.getSubject());
		grid.setText(1, 1, email.getContent());
		previous.setVisible(displayPrevious);
		next.setVisible(displayNext);
	}

	@Override
	protected Widget createWidget() {

		previous = new Button("Previous");
		previous.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(GET_EMAIL_CONTENT, currentIndex - 1);
				controller.handleEvent(e);
			}

		});

		next = new Button("Next");
		next.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(GET_EMAIL_CONTENT, currentIndex + 1);
				controller.handleEvent(e);
			}

		});

		previous.setVisible(false);
		next.setVisible(false);

		grid = new Grid(3, 2);
		grid.setText(0, 0, "Subject: ");
		grid.setText(1, 0, "Content: ");
		grid.setWidget(2, 0, previous);
		grid.setWidget(2, 1, next);
		
		Button goBackToList = new Button("Go Back to List");
		goBackToList.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				Event e = new Event(GET_EMAILS, Session.getAttribute(LOGIN));				
				controller.handleEvent(e);				
			}
			
		});
		
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(grid);
		mainPanel.add(goBackToList);
		
		return mainPanel;
	}

}
